//
//  ThreeViewController.m
//  test
//
//  Created by qianfeng on 15/6/18.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "ThreeViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "AFNetworking.h"
#import "AFSoundManager.h"
@interface ThreeViewController ()<AVAudioPlayerDelegate>
{
    //NSTimer *_timer;
    //NSData *data;
}
@property(nonatomic,strong)AVAudioPlayer *player;
@end

@implementation ThreeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initUI];
    
}

-(void)initUI
{
    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
    button.frame=CGRectMake(20, 80, self.view.frame.size.width-40, 40);
    [button setTitle:@"播放" forState:UIControlStateNormal];
    button.backgroundColor=[UIColor blueColor];
    button.layer.cornerRadius=8;
    button.titleLabel.font=[UIFont boldSystemFontOfSize:20];
    [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    CGRect frame=button.frame;
    frame.origin.y=20+CGRectGetMaxY(frame);
    UIButton *pauseBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    pauseBtn.frame=frame;
    [pauseBtn setTitle:@"暂停" forState:UIControlStateNormal];
    pauseBtn.backgroundColor=[UIColor blueColor];
    pauseBtn.layer.cornerRadius=8;
    pauseBtn.titleLabel.font=[UIFont boldSystemFontOfSize:20];
    [pauseBtn addTarget:self action:@selector(pauseClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:pauseBtn];
    
    
    CGRect frame1=pauseBtn.frame;
    frame1.origin.y=20+CGRectGetMaxY(frame1);
    UIButton *stopBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    stopBtn.frame=frame1;
    [stopBtn setTitle:@"停止" forState:UIControlStateNormal];
    stopBtn.backgroundColor=[UIColor blueColor];
    stopBtn.layer.cornerRadius=8;
    stopBtn.titleLabel.font=[UIFont boldSystemFontOfSize:20];
    [stopBtn addTarget:self action:@selector(stopClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:stopBtn];
    
    
    CGRect frame2=stopBtn.frame;
    frame2.origin.y=20+CGRectGetMaxY(frame2);
    frame2.size.width=80;
    UILabel *label=[[UILabel alloc] initWithFrame:frame2];
    label.text=@"进度条";
    label.font=[UIFont boldSystemFontOfSize:20.0];
    [self.view addSubview:label];
    
    CGRect frame3=label.frame;
    frame3.origin.x=CGRectGetMaxX(frame3)+20;
    frame3.origin.y+=20;
    frame3.size.width=self.view.frame.size.width-CGRectGetMaxX(frame3);
    
    UIProgressView *progress=[[UIProgressView alloc] initWithFrame:frame3];
    progress.progress=0;
    progress.tag=10;
    [self.view addSubview:progress];
    
    CGRect frame4=label.frame;
    frame4.origin.y=CGRectGetMaxY(frame4)+20;
    
    UILabel *vlabel=[[UILabel alloc] initWithFrame:frame4];
    vlabel.text=@"声音";
    vlabel.font=[UIFont boldSystemFontOfSize:20.0];
    [self.view addSubview:vlabel];
    
    
    CGRect frame5=vlabel.frame;
    frame5.origin.x=CGRectGetMaxX(frame5)+20;
    frame5.size.width=175;
    UISlider *slider=[[UISlider alloc] initWithFrame:frame5];
    slider.value=0.3;
    [slider addTarget:self action:@selector(slider:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:slider];
    
    
    
    [NSTimer scheduledTimerWithTimeInterval:1 block:^{
        
        if ([[AFSoundManager sharedManager]areHeadphonesConnected]) {
           
        } else {
           
        }
    } repeats:YES];
    
}
//循环次数player.numberOfLoops =-1 无限循环;//默认只播放一次
-(void)slider:(UISlider *)slider
{
    self.player.volume=slider.value;
}
-(void)btnClick:(UIButton *)button
{
    NSString *url=@"http://fdfs.xmcdn.com/group5/M04/BD/7A/wKgDtlR-oNzQtp5pANFJYo2yYTg023.mp3";
//    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
//    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
//    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        
//        data=[[NSData alloc] initWithData:responseObject];
//
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"失败");
//    }];
//    [self.player play];
    [[AFSoundManager sharedManager]startStreamingRemoteAudioFromURL:url andBlock:^(int percentage, CGFloat elapsedTime, CGFloat timeRemaining, NSError *error, BOOL finished) {
        
        if (!error) {
//            
//            NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
//            [formatter setDateFormat:@"mm:ss"];
//            
//            NSDate *elapsedTimeDate = [NSDate dateWithTimeIntervalSince1970:elapsedTime];
//            _elapsedTime.text = [formatter stringFromDate:elapsedTimeDate];
//            
//            NSDate *timeRemainingDate = [NSDate dateWithTimeIntervalSince1970:timeRemaining];
//            _timeRemaining.text = [formatter stringFromDate:timeRemainingDate];
//            
//            _slider.value = percentage * 0.01;
        } else {
            
            NSLog(@"There has been an error playing the remote file: %@", [error description]);
        }
        
    }];

    
//    if (_timer==nil)
//    {
//        _timer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(reference) userInfo:nil repeats:YES];
//    }
//    [_timer setFireDate:[NSDate date]];
}
-(void)reference
{
    UIProgressView *progress=(UIProgressView *)[self.view viewWithTag:10];
    
    
    progress.progress=self.player.currentTime/self.player.duration;
}
-(void)pauseClick:(UIButton *)button
{
    //[self.player pause];
    [[AFSoundManager sharedManager]pause];
}
-(void)stopClick:(UIButton *)button
{
    //[self.player stop];
    //self.player.currentTime=0.0;
    [[AFSoundManager sharedManager]resume];
}

-(AVAudioPlayer *)player
{

    if (_player==nil)
    {
        NSString *audioPath=[[NSBundle mainBundle] pathForResource:@"mc" ofType:@"mp3"];
        _player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:audioPath] error:nil];
        _player.numberOfLoops=-1;
        _player.volume=0.3;
        _player.delegate=self;
        
    }
    return _player;

    }

#pragma AVAudioPlayerDelegate
//当成功播放一首完成
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    NSLog(@"播放完成");
    
}
//当系统功能介入（打电话过来）播放器被打断，调用此方法
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
    NSLog(@"begin interrupton");
}
//播放器结束被打断。调用
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags
{
    if (player)
    {
        [player play];
    }
}













-(void)findNotification:(NSNotification *)notification
{
    NSDictionary *ary=notification.userInfo;
    self.view.backgroundColor=ary[@"color"];
}


@end
